package com.stu.pdftohtml.spire;

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.utilities.PdfTable;
import com.spire.pdf.utilities.PdfTableExtractor;
import com.spire.xls.ExcelVersion;
import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import org.junit.jupiter.api.Test;

import java.io.FileWriter;
import java.io.IOException;

/**
 * @Author: MI
 * @Date: 2022/2/22/22:45
 * @Description:
 */
public class Demo {

    private String pdfPath = "D:/603912.pdf";
    private String htmlPath = "D:/603912.html";

    @Test
    public void pdf2Html() {
        //加载PDF
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile(pdfPath);
        //保存为HTML格式
        pdf.saveToFile(htmlPath, FileFormat.HTML);
    }

    @Test
    public void pdfToTxt() {

        //创建PdfDocument实例
        PdfDocument doc = new PdfDocument();
        //加载PDF文件
        pdfPath = "d:/1210725478.pdf";
        doc.loadFromFile(pdfPath);
        //创建StringBuilder实例
        StringBuilder sb = new StringBuilder();

        PdfPageBase page;
        //遍历PDF页面，获取每个页面的文本并添加到StringBuilder对象
        for (int i = 0; i < doc.getPages().getCount(); i++) {
            page = doc.getPages().get(i);
            sb.append(page.extractText(true));
        }
        FileWriter writer;
        try {
            //将StringBuilder对象中的文本写入到文本文件
            writer = new FileWriter("d:/ExtractText.txt");
            writer.write(sb.toString());
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

        doc.close();
    }

    /**
     * PDF 提取表格到 excel|不可取，因为有些提取不到 表格
     */
    @Test
    public void pdfGetTable2Excel() {
        //加载示例PDF文档
        pdfPath = "d:/1210725478.pdf";
        String excelPath = "d:/1212361714.xlsx";
        PdfDocument pdf = new PdfDocument(pdfPath);
        //创建一个PdfTableExtractor实例
        PdfTableExtractor extractor = new PdfTableExtractor(pdf);
        //从第一页提取表格
        PdfTable[] pdfTables = extractor.extractTable(0);
        //创建一个工作簿对象
        Workbook wb = new Workbook();
        //删除默认工作表
        wb.getWorksheets().clear();
        //如果找到任何表格
        if (pdfTables != null && pdfTables.length > 0) {
            //循环遍历表格
            for (int tableNum = 0; tableNum < pdfTables.length; tableNum++) {
                //将工作表添加到工作簿
                String sheetName = String.format("Table - %d", tableNum + 1);
                Worksheet sheet = wb.getWorksheets().add(sheetName);
                //循环遍历当前表格中的行
                for (int rowNum = 0; rowNum < pdfTables[tableNum].getRowCount(); rowNum++) {
                    //循环遍历当前表格中的列
                    for (int colNum = 0; colNum < pdfTables[tableNum].getColumnCount(); colNum++) {
                        //从当前表格单元格中提取数据
                        String text = pdfTables[tableNum].getText(rowNum, colNum);
                        //将数据插入特定单元格
                        System.out.print(text + "\t");
                        sheet.get(rowNum + 1, colNum + 1).setText(text);
                    }
                    System.out.println();
                }
                //自动调整列宽
                for (int sheetColNum = 0; sheetColNum < sheet.getColumns().length; sheetColNum++) {
                    sheet.autoFitColumn(sheetColNum + 1);
                }
            }
        }
        //将工作簿保存为 Excel 文件
        // wb.saveToFile("D:/ExportTableToExcel.xlsx", ExcelVersion.Version2016);
        wb.saveToFile(excelPath, ExcelVersion.Version2016);
    }

    /**
     * 提取PDF 表格失败
     *
     * @throws IOException
     */
    @Test
    public void ptfGetTable() throws IOException {
        //实例化PdfDocument类的对象
        PdfDocument pdf = new PdfDocument();

        //加载PDF文档
        pdf.loadFromFile(pdfPath);

        //创建StringBuilder类的实例
        StringBuilder builder = new StringBuilder();

        //创建PdfTableExtractor类的对象
        PdfTableExtractor extractor = new PdfTableExtractor(pdf);

        //遍历每一页
        for (int page = 0; page < pdf.getPages().getCount(); page++) {
            //提取页面中的表格存入PdfTable[]数组
            PdfTable[] tableLists = extractor.extractTable(page);
            if (tableLists != null && tableLists.length > 0) {
                //遍历表格
                for (PdfTable table : tableLists) {
                    int row = table.getRowCount();//获取表格行
                    int column = table.getColumnCount();//获取表格列
                    for (int i = 0; i < row; i++) {
                        for (int j = 0; j < column; j++) {
                            //获取表格中的文本内容
                            String text = table.getText(i, j);

                            //将获取的text写入StringBuilder容器
                            builder.append(text + " ");
                        }
                        builder.append("\r\n");
                    }
                }
            }
        }
        //保存为txt文档
        FileWriter fileWriter = new FileWriter("d:/ExtractedTable.txt");
        fileWriter.write(builder.toString());
        fileWriter.flush();
        fileWriter.close();
    }
}
